package com.gzzhsdcm.czh.myxw.frgment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.gzzhsdcm.czh.myxw.R;
import com.gzzhsdcm.czh.myxw.activity.Webactivity;
import com.gzzhsdcm.czh.myxw.adapter.ShoyeAdapter;
import com.gzzhsdcm.czh.myxw.getset.ShoyeGetset;
import com.gzzhsdcm.czh.myxw.view.MyListView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import static com.gzzhsdcm.czh.myxw.httpurl.HttpUrl.XINGWEN;

/**
 * @fileName ShoyeFragment
 * @date on 2018-12-21 15:21
 * @company 贵州智合时代传媒有限公司
 * @developers czh
 * @email 1632365610@qq.com
 */
@ContentView(R.layout.fragment_shoye)
public class ShoyeFragment extends Fragment {
    @ViewInject(R.id.list_shoye)
    private MyListView listView;
    private List<ShoyeGetset.ResultBean.DataBean> list = new ArrayList<>();
    private String index;
    private ShoyeAdapter adapter;
    private String KET = "bc8cb72adb403d7ccf7f4ed0a6061928";
    public ShoyeFragment setIndex(String index) {
        this.index = index;
        return this;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return x.view().inject(this,inflater,container);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();


                httpQingqiu();


    }

    private void httpQingqiu() {
        OkGo.<String>post(XINGWEN)
                .params("type",index)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.d("TAG",response.body()+index);
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            for (int i = 0;i<jsonArray.length();i++){
                               ShoyeGetset.ResultBean.DataBean dataBean = new ShoyeGetset.ResultBean.DataBean();
                                JSONObject object = jsonArray.getJSONObject(i);

                               dataBean.setThumbnail_pic_s(object.optString("thumbnail_pic_s"));
                               dataBean.setThumbnail_pic_s02(object.getString("thumbnail_pic_s02"));
                               dataBean.setThumbnail_pic_s03(object.optString("thumbnail_pic_s03"));
                               dataBean.setAuthor_name(object.optString("author_name"));
                               dataBean.setUrl(object.optString("url"));
                               dataBean.setCategory(object.optString("category"));
                               dataBean.setTitle(object.optString("title"));
                               dataBean.setDate(object.optString("date"));
                               list.add(dataBean);
                            }
                            adapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void initView() {
        adapter = new ShoyeAdapter(getActivity(),list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), Webactivity.class);
                intent.putExtra("webUrl",list.get(i).getUrl());
                startActivity(intent);
            }
        });
    }
}
